package com.beiye.drivertransport.SubActivity;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.ExaminationPhotosBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.githang.statusbar.StatusBarCompat;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.squareup.picasso.Picasso;

import java.util.List;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

//考试拍照界面
   public class ExaminationPhotosActivity extends TwoBaseAty {


    GridView gv_photo;

    ImageView img_photo_back;


    @Override
    public int getLayoutId() {
        return R.layout.activity_examination_photos;

    }

    public void onClick(View view) {
        if (view.getId() == R.id.img_photo_back) {
            finish();
        }

    }
    @Override
    public void initData() {
        gv_photo = (GridView) findViewById(R.id.gv_photo);
        img_photo_back = (ImageView) findViewById(R.id.img_photo_back);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
    }

    @Override
    public void requestData() {
        Bundle bundle = getIntent().getExtras();
        //通过name关键字取出用户名
        int    sn = bundle.getInt("uqpSn");

        //查询考试照片
        OkGo.get(AppInterfaceConfig.BASE_URL+"course/userQpPhoto/"+sn)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String result, Call call, Response response) {

                        ExaminationPhotosBean examinationPhotosBean = JSON.parseObject(result, ExaminationPhotosBean.class);
                        List<ExaminationPhotosBean.RowsBean> rows = examinationPhotosBean.getRows();
                        if(rows.size()==0)
                        {
                            return;
                        }

                        ExaminationPhotosApt  examinationPhotosApt = new ExaminationPhotosApt(ExaminationPhotosActivity.this, rows, R.layout.gv_test_photo_item);
                        gv_photo.setAdapter(examinationPhotosApt);

                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);

                    }
                });

    }

    public class ExaminationPhotosApt extends CommonAdapter<ExaminationPhotosBean.RowsBean> {
        private final List<ExaminationPhotosBean.RowsBean> mList;
        private Context mContext;

        public ExaminationPhotosApt(Context context, List<ExaminationPhotosBean.RowsBean> mList, int itemLayoutId) {
            super(context, mList, itemLayoutId);
            this.mContext = context;
            this.mList = mList;
        }


        @Override
        public void convert(ViewHolder holder, ExaminationPhotosBean.RowsBean item, final int position) {
            ExaminationPhotosBean.RowsBean rowsBean = mList.get(position);
            String url = rowsBean.getUrl();
            ImageView img_photo = holder.getView(R.id.img_parent);

            Picasso.with(mContext).load(Uri.parse(url)).placeholder(R.mipmap.no_data).into(img_photo);

        }


    }
}
